package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Destination;
import cn.wolfcode.wolf2w.domain.Region;
import cn.wolfcode.wolf2w.repository.RegionRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.query.RegionQuery;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class RegionServiceImpl implements IRegionService {
    @Autowired
    private RegionRepository regionRepository;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private MongoTemplate template;
    @Override
    public void save(Region region) {
        region.setId(null);
        regionRepository.save(region);
    }

    @Override
    public void update(Region region) {
        // 需要查询数据再修改
        Region region1 = this.get(region.getId());
        if (!StringUtils.hasLength(region.getName())) {
            region.setName(region1.getName());
        }
        if (!StringUtils.hasLength(region.getSn())) {
            region.setSn(region1.getSn());
        }
        if (!StringUtils.hasLength(region.getInfo())) {
            region.setInfo(region1.getInfo());
        }
        if (region.getRefIds().isEmpty()) {
           region.setRefIds(region1.getRefIds());
        }
        regionRepository.save(region);
    }

    @Override
    public void delete(String id) {
        regionRepository.deleteById(id);
    }

    @Override
    public Region get(String id) {
        return regionRepository.findById(id).orElse(null);
    }

    @Override
    public List<Region> listAll() {
        return regionRepository.findAll();
    }

    @Override
    public Page<Region> query(RegionQuery qo) {
        // 用于添加where条件的
        Query query = new Query();
        // 分页条件
        Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.ASC, "_id");
        // 把条件放到query对象中
        return DBHelper.query(template,pageable,query,Region.class);
    }

    @Override
    public void saveOrUpdate(Region region) {
        if (StringUtils.hasLength(region.getId())) {
            this.update(region);
        }else {
            this.save(region);
        }
    }

    @Override
    public void changeHotValue(int hot, String id) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(id));
        Update update = new Update();
        update.set("ishot",hot);
        template.updateMulti(query, update,Region.class);
    }

    @Override
    public List<Region> queryHotRegion() {
        Query query = new Query();
        query.addCriteria(Criteria.where("ishot").is(1));
        List<Region> list = template.find(query, Region.class);
        return list;
    }
}
